package com.university.mentalhealthplatform.studentV3.controller;

import com.university.mentalhealthplatform.appModels.Detail;
import com.university.mentalhealthplatform.studentV3.service.DetailServiceV3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v3/details")
public class DetailControllerV3 {

    @Autowired
    private DetailServiceV3 detailServiceV3;

    // 获取所有详细信息
    @GetMapping
    public List<Detail> getAllDetails() {
        return detailServiceV3.getAllDetails();
    }

    // 根据类型获取详细信息
    @GetMapping("/{type}")
    public ResponseEntity<Detail> getDetailByType(@PathVariable String type) {
        Detail detail = detailServiceV3.getDetailByType(type);

        if (detail != null) {
            return ResponseEntity.ok(detail);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 添加新的详细信息
    @PostMapping
    public ResponseEntity<Detail> createDetail(@RequestBody Detail detail) {
        Detail createdDetail = detailServiceV3.saveDetail(detail);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdDetail);
    }

    // 更新现有详细信息
    @PutMapping("/{type}")
    public ResponseEntity<Detail> updateDetail(@PathVariable String type, @RequestBody Detail detail) {
        Detail updatedDetail = detailServiceV3.updateDetail(type, detail);

        if (updatedDetail != null) {
            return ResponseEntity.ok(updatedDetail);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 删除详细信息
    @DeleteMapping("/{type}")
    public ResponseEntity<Void> deleteDetail(@PathVariable String type) {
        if (detailServiceV3.deleteDetail(type)) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    // 在 DetailController 类中添加以下方法

    // 获取介绍文本
    @GetMapping("/introduction")
    public ResponseEntity<String> getIntroduction() {
        try {
            String introductionText = detailServiceV3.getIntroductionText();
            if (introductionText != null) {
                return ResponseEntity.ok(introductionText); // 直接返回字符串
            } else {
                return ResponseEntity.notFound().build(); // 404 响应
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("Internal Server Error");
        }
    }


}

